package com.sojson.project.controller.param.verify;

import java.util.List;

import com.sojson.enums.core.EGeneralYesNo;
import com.sojson.project.entity.bo.OrderGoodsParam;
import com.sojson.project.entity.bo.OrderParam;
import com.sojson.project.entity.bo.OrderStoreParam;
import com.sojson.project.entity.bo.OrderUserParam;
import com.sojson.project.entity.po.HotelOrder;
import com.sojson.result.Result;
import com.sojson.util.ExpUtil;
import com.sojson.util.MatcheUtil;
import com.sojson.util.StringUtil;

/**
 * 入驻记录信息控制器参数验证
 * 
 * @author liu
 * @date 2021-07-02
 */
public class HotelOrderParamVerify {

    /**
     * 单条插入
     * 
     * @param dto
     * @return
     */
    public static Result<Integer> insert(HotelOrder dto) {
        if (StringUtil.isBlank(dto.getNoRoom())) {
            return Result.fail("房间编号不能为空！");
        }
        if (StringUtil.isBlank(dto.getStartTime())) {
            return Result.fail("开始时间不能为空！");
        }
        if (StringUtil.isBlank(dto.getEndTime())) {
            return Result.fail("结束时间不能为空！");
        }

        return Result.success();
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param dto
     * @return
     */
    public static Result<Integer> updateById(HotelOrder dto) {
        if (StringUtil.isBlank(dto.getId())) {
            return Result.fail("ID不能为空！");
        }

        return Result.success();
    }

    /**
     * 下单
     * 
     * @param orderParam
     * @return
     */
    public static Result<?> orders(OrderParam orderParam) {
        List<OrderStoreParam> orderStoreParams = orderParam.getOrderStoreParams();
        if (StringUtil.isBlankObject(orderStoreParams) || orderStoreParams.size() < 1) {
            ExpUtil.throwEx("商家订单参数集合不能为空！");
        }
        int isShop = orderStoreParams.size() > 1 ? EGeneralYesNo.YES.getCode() : EGeneralYesNo.NO.getCode();
        orderParam.setIsShop(isShop);
        if (orderStoreParams.size() > 1) {
            ExpUtil.throwEx("暂不支持购物车支付！");
        }
        for (OrderStoreParam orderStoreParam : orderStoreParams) {
            orderStoreParam.setIsShop(isShop);
            String couponNos = orderStoreParam.getCouponNos();
            if (StringUtil.isBlankString(couponNos) || "0".equals(couponNos)) {
                orderStoreParam.setCouponNos(null);
            }
            List<OrderGoodsParam> orderGoodsParams = orderStoreParam.getOrderGoodsParams();
            if (orderGoodsParams.size() > 1) {
                ExpUtil.throwEx("暂不支持购物车支付！");
            }
            if (StringUtil.isBlankObject(orderStoreParam.getStartTime())) {
                ExpUtil.throwEx("开始时间不能为空！");
            }
            if (StringUtil.isBlankObject(orderStoreParam.getEndTime())) {
                ExpUtil.throwEx("结束时间不能为空！");
            }
            if (StringUtil.isBlankObject(orderGoodsParams) || orderGoodsParams.size() < 1) {
                ExpUtil.throwEx("商品订单参数集合不能为空！");
            }
            for (OrderGoodsParam orderGoodsParam : orderGoodsParams) {
                orders(orderGoodsParam);
            }
        }

        return Result.success();
    }

    /**
     * 下单
     * 
     * @param orderGoodsParam
     * @return
     */
    public static void orders(OrderGoodsParam orderGoodsParam) {
        if (StringUtil.isBlankObject(orderGoodsParam.getGoodSpecsNo())) {
            ExpUtil.throwEx("房间编号不能为空！");
        }
        Integer goodNum = orderGoodsParam.getGoodNum();
        if (StringUtil.isBlankObject(goodNum) || goodNum < 1) {
            ExpUtil.throwEx("商品数量不能为空！");
        }
        String mobile = orderGoodsParam.getMobile();
        if (StringUtil.isBlankObject(mobile)) {
            ExpUtil.throwEx("用户手机号不能为空！");
        }
        if (!MatcheUtil.isMobile(mobile)) {
            ExpUtil.throwEx("手机号格式错误！");
        }
        List<OrderUserParam> orderUsers = orderGoodsParam.getOrderUsers();
        if (StringUtil.isBlankObject(orderUsers) || orderUsers.size() < 1 || orderUsers.size() != goodNum) {
            ExpUtil.throwEx("用户参数集合不能为空！");
        }
        for (OrderUserParam orderUser : orderUsers) {
            orders(orderUser, mobile);
        }
    }

    /**
     * 下单
     * 
     * @param orderGoodsParam
     * @param mobile            手机号
     * @return
     */
    public static void orders(OrderUserParam orderUser, String mobile) {
        if (StringUtil.isBlankObject(orderUser.getName())) {
            ExpUtil.throwEx("用户姓名不能为空！");
        }
        orderUser.setMobile(mobile);
    }

}